package xyz.ddlnt.serviceusr.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import xyz.ddlnt.commonutil.constant.FieldConstants;
import xyz.ddlnt.model.entity.ExamQuestion;
import xyz.ddlnt.model.vo.ExamQuestionIntroVO;
import xyz.ddlnt.serviceusr.mapper.ExamQuestionMapper;
import xyz.ddlnt.serviceusr.service.ExamQuestionService;

import java.util.List;

/**
 * @author ZZULI_SE 210910
 * @data 2025/5/5 1:59
 */
@Service
public class ExamQuestionServiceImpl extends ServiceImpl<ExamQuestionMapper, ExamQuestion> implements ExamQuestionService {

    @Resource
    private  ExamQuestionMapper examQuestionMapper;
    @Override
    public List<ExamQuestionIntroVO> listExamQuestionIntroVO(Long technologyId) {
        LambdaQueryWrapper<ExamQuestion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(i->{i.
                        eq(ExamQuestion::getTechnologyId, technologyId)
                        .eq(ExamQuestion::getPublishStatus, FieldConstants.PUBLISHED);
                })
                .select(ExamQuestion::getId, ExamQuestion::getQuestionTitle, ExamQuestion::getTags, ExamQuestion::getExamQuesDifficulty);
        List<ExamQuestion> examQuestions = examQuestionMapper.selectList(queryWrapper);
        return examQuestions.stream().map(examQuestion -> {
            ExamQuestionIntroVO examQuestionIntroVO = new ExamQuestionIntroVO();
            BeanUtils.copyProperties(examQuestion, examQuestionIntroVO);
            if (!StringUtils.isEmpty(examQuestion.getTags())) {
                examQuestionIntroVO.setTags(JSONArray.parseArray(examQuestion.getTags()));
            }
            return examQuestionIntroVO;
        }).toList();
    }

    @Override
    public List<ExamQuestionIntroVO> listExamQuestionIntroDir(Long technologyId) {
        LambdaQueryWrapper<ExamQuestion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(i->{i.
                        eq(ExamQuestion::getTechnologyId, technologyId)
                        .eq(ExamQuestion::getPublishStatus, FieldConstants.PUBLISHED);
                })
                .select(ExamQuestion::getId, ExamQuestion::getQuestionTitle, ExamQuestion::getPublishTime, ExamQuestion::getUpdateTime);
        List<ExamQuestion> examQuestions = examQuestionMapper.selectList(queryWrapper);
        return examQuestions.stream().map(examQuestion -> {
            ExamQuestionIntroVO examQuestionIntroVO = new ExamQuestionIntroVO();
            BeanUtils.copyProperties(examQuestion, examQuestionIntroVO);
            return examQuestionIntroVO;
        }).toList();
    }
}
